// https://leetcode.cn/problems/intersection-of-two-arrays-ii/
// Created by ade on 2022/8/3.
//
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_set>

using namespace std;
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_multiset<int> s;
        for(auto i : nums1){
            s.insert(i);
        }
        vector<int> res = {};
        sort(nums2.begin(), nums2.end());
        for(auto i : nums2){
            auto iter = s.find(i);
            if(iter != s.end()){
                res.push_back(i);
                s.erase(iter);
            }
        }
        return res;
    }
};

int main() {
    Solution so;
    vector<int> nums1 = {1, 2, 2, 1};
    vector<int> nums2 = {2, 2};
    auto res = so.intersect(nums1, nums2);
    for (auto i : res) {
        cout << i << ",";
    }
    cout << endl;
    return 0;
}

